import 'package:dio/dio.dart';
import 'package:flutter_juwo/common/common_index.dart';
import 'package:flutter/material.dart';
import 'package:flutter_juwo/ui/custom/select_pic_widget.dart';
import 'package:flutter_juwo/ui/custom/select_show_pic_widget.dart';
import 'package:flutter_juwo/ui/model/base_entity.dart';
import 'package:flutter_juwo/ui/utils/toast_utils.dart';
import 'package:flutter_juwo/ui/utils/utils.dart';

import 'model/banks_entity.dart';
import 'model/realname_infor_entity.dart';

///实名信息
class RealNameInfor extends StatefulWidget {
  @override
  _RealNameInforState createState() => _RealNameInforState();
}

class _RealNameInforState extends State<RealNameInfor> {
  MyPageState ps = MyPageState.LOADING;
  BanksEntity _banksEntity;
  RealnameInforEntity _realnameInforEntity;

  TextEditingController _nameContr; //姓名
  TextEditingController _idNumContr; //身份证号
  TextEditingController _bankNumContr; //银行卡号
  TextEditingController _wxContr; //微信号
  TextEditingController _qqContr; //qq号
  String bankName = "";
  String picFont = "";
  String picBack = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.getData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: UiUtils.appBar(context, "实名信息"),
      body: getBody(),
    );
  }

  getBody() {
    if (ps == MyPageState.LOADING) {
      return LoadingPage();
    }

    if (ps == MyPageState.HAVE_DATA) {
      return getContent();
    }
    return Container();
  }

  void getData() {
    DioManager.getInstance().get(Api.USER_GETALLBANK, null, (json) {
      _banksEntity = BanksEntity.fromJson(json);
      if (null == _banksEntity) {
        ToastUtil.showShortErrToast(Api.ERR_NOTHING);
        NavigatorUtils.goBack(context);
        return;
      }

      if (_banksEntity.code != 200) {
        ToastUtil.showShortErrToast(_banksEntity.message);
        NavigatorUtils.goBack(context);
        return;
      }
      getUserInfor();
    }, (err) {
      ToastUtil.showShortErrToast(Api.ERR_TIME_OUT);
      NavigatorUtils.goBack(context);
      return;
    });
  }

  ///获取用户实名认证信息
  void getUserInfor() {
    DioManager.getInstance().get(Api.USER_USERINFORLIST, null, (json) {
      _realnameInforEntity = RealnameInforEntity.fromJson(json);
      if (null == _realnameInforEntity) {
        ToastUtil.showShortErrToast(Api.ERR_NOTHING);
        NavigatorUtils.goBack(context);
        return;
      }

      if (_realnameInforEntity.code != 200) {
        ToastUtil.showShortErrToast(_realnameInforEntity.message);
        NavigatorUtils.goBack(context);
        return;
      }

      ps = MyPageState.HAVE_DATA;

      _nameContr =
          TextEditingController(text: _realnameInforEntity.data.identityName);
      _idNumContr =
          TextEditingController(text: _realnameInforEntity.data.identityNumber);
      _bankNumContr =
          TextEditingController(text: _realnameInforEntity.data.bankNumber);
      _wxContr = TextEditingController(text: _realnameInforEntity.data.wx);
      _qqContr = TextEditingController(text: _realnameInforEntity.data.qq);

      bankName = _realnameInforEntity.data.bankName;
      picFont = _realnameInforEntity.data.identityFront;
      picBack = _realnameInforEntity.data.identityAfter;

      setState(() {});
    }, (err) {
      ToastUtil.showShortErrToast(Api.ERR_TIME_OUT);
      NavigatorUtils.goBack(context);
      return;
    });
  }

  getContent() {
    return Column(
      children: <Widget>[
        UiUtils.divider(height: 2),
        Expanded(
          flex: 1,
          child: ListView(
            children: <Widget>[getTop(), getMiddle()],
          ),
        ),
        getBottom()
      ],
    );
  }

  getTop() {
    return Container(
      margin: EdgeInsets.all(ScreenUtil().setWidth(30)),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8), color: Colours.white),
      child: Column(
        children: <Widget>[
          getRow1("姓名", _nameContr),
          getRow1("身份证号", _idNumContr),
          getRow2(),
          getRow3(),
          getRow1("银行卡号", _bankNumContr),
        ],
      ),
    );
  }

  getMiddle() {
    return Container(
      margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(30), 0,
          ScreenUtil().setWidth(30), ScreenUtil().setWidth(30)),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(8), color: Colours.white),
      child: Column(
        children: <Widget>[
          getRow1("微信号", _wxContr),
          getRow1("QQ号", _qqContr),
        ],
      ),
    );
  }

  ///显示文本可输入
  getRow1(
    String left,
    TextEditingController controller,
  ) {
    return Container(
      margin: EdgeInsets.all(ScreenUtil().setWidth(10)),
      child: Row(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Container(
              child: Text("$left"),
            ),
          ),
          Expanded(
            flex: 2,
            child: Container(
              child: UiUtils.myTextField(context, controller),
            ),
          ),
        ],
      ),
    );
  }

  //显示图片
  getRow2() {
    return Container(
      margin: EdgeInsets.all(ScreenUtil().setWidth(10)),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            child: Text("身份证正反面"),
          ),
          Row(
            children: <Widget>[
              Container(
                  height: ScreenUtil().setWidth(200),
                  width: ScreenUtil().setWidth(200),
                  margin: EdgeInsets.fromLTRB(0, ScreenUtil().setWidth(30), 0,
                      ScreenUtil().setWidth(30)),
                  child: SelectShowPicWidget(
                    1,
                    (value) {
                      print("回调了吗：${value.length}");
                      if(value.length>0){
                        picFont = value[0];
                        _realnameInforEntity.data.identityFront = value[0];
                      }else{
                        picFont = "";
                        _realnameInforEntity.data.identityFront = "";
                      }
                      setState(() {});
                    },
                    imgUrls: _realnameInforEntity == null ||
                            _realnameInforEntity.data.identityFront.isEmpty
                        ? []
                        : [_realnameInforEntity.data.identityFront],
                    isHead: false,
                  )),
              Container(
                  height: ScreenUtil().setWidth(200),
                  width: ScreenUtil().setWidth(200),
                  margin: EdgeInsets.fromLTRB(ScreenUtil().setWidth(20),
                      ScreenUtil().setWidth(30), 0, ScreenUtil().setWidth(30)),
                  child: SelectShowPicWidget(
                    1,
                    (value) {
                      print("回调了吗2：${value.length}");
                      if(value.length>0){
                        picBack = value[0];
                        _realnameInforEntity.data.identityAfter = value[0];
                      }else{
                        picBack = "";
                        _realnameInforEntity.data.identityAfter="";
                      }

                    },
                    imgUrls: _realnameInforEntity.data.identityAfter.isEmpty
                        ? []
                        : [_realnameInforEntity.data.identityAfter],
                    isHead: false,
                  )),
            ],
          )
        ],
      ),
    );
  }

  //显示文本 可选择
  getRow3() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(10),
          ScreenUtil().setWidth(10),
          ScreenUtil().setWidth(10),
          ScreenUtil().setWidth(30)),
      child: Row(
        children: <Widget>[
          Expanded(
            flex: 1,
            child: Container(
              child: Text("所属银行"),
            ),
          ),
          Expanded(
            flex: 2,
            child: GestureDetector(
              onTap: () {
                _showSelectAccountTypeDialog(_banksEntity);
              },
              child: Container(
                decoration: BoxDecoration(
                    color: Colours.c243,
                    borderRadius: BorderRadius.circular(6)),
                alignment: Alignment.centerLeft,
                padding: EdgeInsets.fromLTRB(
                    ScreenUtil().setWidth(30),
                    ScreenUtil().setWidth(18),
                    ScreenUtil().setWidth(20),
                    ScreenUtil().setWidth(18)),
                child: Text(
                  bankName,
                  style: TextStyles.custom(
                      34, Colours.appC1, Font.PingFangRegular),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  _showSelectAccountTypeDialog(BanksEntity valueList) {
    List<Widget> widgets = [];

    for (int i = 0; i < valueList.data.length; i++) {
      widgets.add(UiUtils.divider());
      widgets.add(InkWell(
        onTap: () {
          bankName = valueList.data[i].bankName;
          setState(() {});
          NavigatorUtils.goBack(context);
        },
        child: Container(
          height: ScreenUtil().setWidth(90),
          alignment: Alignment.center,
          child: Text(
            "${valueList.data[i].bankName}",
            style: TextStyles.custom(
                28, Colours.appC16, Font.PingFangMedium),
          ),
        ),
      ));
    }

    showElasticDialog(
        context: context,
        builder: (BuildContext context) {
          return Material(
            type: MaterialType.transparency,
            child: Center(
              child: Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(8.0),
                ),
                width: ScreenUtil().setWidth(540),
                height: ScreenUtil().setWidth(700),
                padding: EdgeInsets.only(top: ScreenUtil().setWidth(48)),
                child: Theme(
                  data: ThemeData(
                    buttonTheme: ButtonThemeData(
                      minWidth: double.infinity,
                    ),
                    textTheme: TextTheme(
                        button: TextStyle(
                      fontSize: Dimens.font_sp14,
                    )),
                  ),
                  child: Column(
                    children: <Widget>[
                      Container(
                        alignment: Alignment.center,
                        height: ScreenUtil().setWidth(78),
                        child: Text(
                          "选择银行",
                          style: TextStyles.custom(
                              30, Colours.appC1, Font.PingFangBlod),
                        ),
                      ),
                      Expanded(
                        flex: 1,
                        child: ListView(
                          children: widgets,
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ),
          );
        });
  }

  getBottom() {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(30),
          ScreenUtil().setWidth(60)),
      child: UiUtils.bottomBotton("提交", () {
        ///1.检查数据

        if (checkData()) {
          ///2.提交数据
          UiUtils.showMyDialog(context, text: "正在提交...");
          updata();
        }
      }),
    );
  }

  void updata() {
    FormData params = FormData.from({
      "identity_name": _nameContr.text,
      "identity_number": _idNumContr.text,
      "bank_number": _bankNumContr.text,
      "wx": _wxContr.text,
      "qq": _qqContr.text,
      "bank_name": bankName,
      "identity_front": picFont,
      "identity_after": picBack,
    });

    DioManager.getInstance().post(Api.USER_USERINFORADD, params, (json) {
      BaseEntity entity = BaseEntity.fromJson(json);

      if (null == entity) {
        ToastUtil.showShortErrToast(Api.ERR_NOTHING);
        NavigatorUtils.goBack(context);
        return;
      }
      if (entity.code != 200) {
        ToastUtil.showShortErrToast(entity.message);
        NavigatorUtils.goBack(context);
        return;
      }
      NavigatorUtils.goBack(context);
      ToastUtil.showShortToast("提交成功");
      NavigatorUtils.goBackWithParams(context, true);
    }, (err) {
      ToastUtil.showShortErrToast(Api.ERR_TIME_OUT);
      NavigatorUtils.goBack(context);
    });
  }

  bool checkData() {
    if (_nameContr.text.isEmpty) {
      ToastUtil.showShortErrToast("姓名不能为空!");
      return false;
    }
    if (_idNumContr.text.isEmpty) {
      ToastUtil.showShortErrToast("身份证号不能为空!");
      return false;
    }

    if (picFont.isEmpty) {
      ToastUtil.showShortErrToast("请选择身份证正面!");
      return false;
    }
    if (picBack.isEmpty) {
      ToastUtil.showShortErrToast("请选择身份证反面!");
      return false;
    }

    if (bankName.isEmpty) {
      ToastUtil.showShortErrToast("请选择所属银行!");
      return false;
    }

    if (_bankNumContr.text.isEmpty) {
      ToastUtil.showShortErrToast("银行卡号不能为空!");
      return false;
    }
    if (_wxContr.text.isEmpty) {
      ToastUtil.showShortErrToast("微信号不能为空!");
      return false;
    }
    if (_qqContr.text.isEmpty) {
      ToastUtil.showShortErrToast("QQ号不能为空!");
      return false;
    }

    return true;
  }
}
